//
//  TF_LivingAskCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/3/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TF_LivingAskCell: UITableViewCell {
    
    @objc public var viewWidth:CGFloat = 0
    @objc public var viewHeight:CGFloat = 0
    /**提问人的头像*/
    @objc public var questionerAvatar = UIImageView()
    @objc public let underline = UIView()
    @objc public let name = UILabel()
    @objc public let time = UILabel()
    //聊天内容
    @objc public let content = UILabel()
    //回复的窗口
    @objc public let replyView = UIView()
     //角色
    @objc public let roleLabel = UILabel()
   
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
      
        questionerAvatar.clipsToBounds = true
        questionerAvatar.layer.masksToBounds = true

        self.questionerAvatar.layer.borderColor = UIColor(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1.0).cgColor
        self.questionerAvatar.layer.borderWidth = 0.5;
        self.contentView.addSubview(self.questionerAvatar)

     
        self.underline.backgroundColor = UIColor(red: 235/255.0, green: 239/255.0, blue: 242/255.0, alpha: 1.0)
        self.contentView.addSubview(self.underline)



        //其它人的名字
   
        self.name.backgroundColor = UIColor.clear
        self.name.numberOfLines = 0;
        self.contentView.addSubview(self.name)
    
         //角色

        self.roleLabel.text = "主播"
        self.roleLabel.textAlignment = .center;
        self.roleLabel.font = UIFont.PingFang(size: 12)
        self.roleLabel.textColor = UIColor(red: 1/255.0, green: 94/255.0, blue: 255/255.0, alpha: 1.0)
        self.roleLabel.backgroundColor = UIColor.clear
        self.roleLabel.numberOfLines = 0;
        self.contentView.addSubview(self.roleLabel)
    
        //时间
        self.time.backgroundColor = UIColor.clear
        self.time.numberOfLines = 0;
        self.contentView.addSubview(self.time)

        //聊天内容
        self.content.isHidden = true
        //防止自动换行
        self.content.lineBreakMode = .byCharWrapping
        self.content.font = UIFont.PingFang(size: 16)
        self.content.backgroundColor = UIColor.clear
        self.content.numberOfLines = 0;
        self.contentView.addSubview(self.content)
        
 
        self.contentView.addSubview(self.replyView)
     
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //头像图片
        self.questionerAvatar.frame = CGRect(x: 15, y: 11, width: 30, height: 30)

        self.questionerAvatar.layer.cornerRadius =  self.questionerAvatar.bounds.size.width / 2;
        //下划线
        self.underline.frame = CGRect(x: 15, y: self.frame.size.height - 1, width: self.frame.size.width - 30, height: 1)
        
    }
    
    
    @objc public var model:TFQuestionData?{
        didSet {
            if ((model) != nil)
            {
                self.name.isHidden = false
                self.content.isHidden = false
                self.time.isHidden = false


                //时间
                let time = model!.time as NSString

                let currentDateStr = TalkfunSharedTools.getTimestamp(mStr: time)

                let str = currentDateStr as NSString
     
                let othersNameString = self.getAttributedString(str: str, font: UIFont.PingFang(size: 14) as Any as! UIFont, color: UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0))
              
                self.time.attributedText = othersNameString
             
                let nickname:NSString = model!.nickname as NSString
                if (nickname.length > 5 ) {

                    let nickname:NSString = model!.nickname as NSString
                    
                    var newNickname:NSString = nickname.substring(with: NSRange(location: 0, length: 5)) as NSString
                    
                    newNickname = newNickname as String + "..." as NSString
                    
                    model!.nickname =  newNickname as String
                }

                
                let attr17: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 17) as Any,.foregroundColor: UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0), ]
                
                let nickName:NSString = model!.nickname as NSString
                
                let size = nickName.size(withAttributes: attr17)
                
                let  timeW:CGFloat = 48
                var  nameW:CGFloat = size.width ;
                if (size.width > self.viewWidth  - timeW - 14 - 14) {
                         nameW = self.viewWidth - 14 - 14 - timeW;
                }
                    
                let nameString = NSMutableAttributedString.init(string: nickName as String, attributes: attr17)

                    
                self.name.attributedText = nameString
              
                
                let othersNameX:CGFloat = 45 + 7
                let othersNameY:CGFloat = 14.5;
                let nameH:CGFloat = 24
                self.name.frame = CGRect(x: othersNameX, y: othersNameY, width: nameW, height: nameH)
                
                //主播 || 助教
                if model!.role == .roleSpadmin || model!.role == .roleAdmin {
                    
        
                               if (model!.role == .roleSpadmin)
                               {
                                   self.roleLabel.text = "主播";
                               }else if (model!.role == .roleAdmin)
                               {
                                    self.roleLabel.text = "助教";
                               }
                             
                    self.roleLabel.isHidden = false
                    self.roleLabel.frame = CGRect(x: self.name.frame.maxX + 7.5, y: 17.5, width: 37, height: 18)
    
                    self.roleLabel.backgroundColor = UIColor(red: 223/255.0, green: 247/255.0, blue: 55/255.0, alpha: 1.0)

                   self.roleLabel.clipsToBounds = true
                   self.roleLabel.layer.cornerRadius =  self.roleLabel.frame.size.height/2.0;

                        
                    }else{
                        self.roleLabel.isHidden = true;
                        self.roleLabel.frame = self.name.frame;
                        self.roleLabel.backgroundColor = UIColor.clear
                    }

                 let timeH:CGFloat = nameH
                 let timeX:CGFloat = self.roleLabel.frame.maxX + 10
                 let timeY:CGFloat = othersNameY
                self.time.frame = CGRect(x: timeX, y: timeY, width: timeW, height: timeH)
            
                let attr16: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 16) as Any,.foregroundColor: UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1.0), ]
                let content:NSString = model!.content as NSString
                let contentString = NSMutableAttributedString.init(string: content as String, attributes: attr16)
                
              
                let size16 = TalkfunSharedTools.getSizeLabel(labelString: contentString, labelWidth:self.viewWidth - 52 - 19, labelfont: UIFont.PingFang(size: 16))
   
                
                
                self.content.attributedText = contentString
                self.content.frame = CGRect(x: self.name.frame.origin.x, y: 41.5, width: self.viewWidth - 52 - 19, height: size16.height)
  
                self.name.textAlignment    = .left;
                self.content.textAlignment =  .left;
             
                //删除所有view
                for v in self.replyView.subviews as [UIView] {
                       v.removeFromSuperview()
                   }
   
                    //有回复
                if (model?.answer.count)! > 0 {
                      
                    let answer = model!.answer;
                    //所有回复的高度
                     var contentHight:CGFloat = 0;
                    var  vcY:CGFloat = 0;
                    for i in stride(from: 0, to: (model?.answer.count)!  ,by: 1) {

                        let dict1 =  answer[i] as! TFQuestionAnswer
                        

                        let attr16_2: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 16) as Any,.foregroundColor: UIColor.white, ]
                        let string2 = NSMutableAttributedString.init(string: dict1.content as String, attributes: attr16_2)
                        
                        let size2 = TalkfunSharedTools.getSizeLabel(labelString: string2, labelWidth:self.viewWidth - 79 - 19, labelfont: UIFont.PingFang(size: 16))
                        
                        let vc   = UIView()
                        vc.backgroundColor = UIColor.clear
                        //当前第一个回复的总高度
                        let vcH:CGFloat  = 33 + size2.height

                        vc.frame = CGRect(x: 0, y: vcY, width: self.replyView.frame.size.width, height: vcH)
                                  //5 是间距
                                  vcY     = vcH + contentHight + 5
                            contentHight  = contentHight + vcH
                        
                        self.replyView.addSubview(vc)
                 
                          //回复的老师的头像
                        let avatarImage1 = UIImageView()
                        avatarImage1.frame = CGRect(x: self.content.frame.origin.x, y: 0, width: 20, height: 20)
                        
                
                        avatarImage1.layer.cornerRadius =  avatarImage1.bounds.size.width / 2;
                        let avatarImageStr1 = dict1.avatar as NSString;
                        avatarImage1.sd_setImage(with: NSURL.init(string: avatarImageStr1 as String) as URL?, completed: nil)

                        vc.addSubview(avatarImage1)
                        
                        //其它人的名字
                        let nameLaberl = UILabel()
                        nameLaberl.backgroundColor = UIColor.clear
                        nameLaberl.numberOfLines = 0;
                        vc.addSubview(nameLaberl)
                        
                        //主播
                        let roleLaberl = UILabel()
                        roleLaberl.textAlignment = .center;
                        roleLaberl.numberOfLines = 0;
                        vc.addSubview(roleLaberl)
                  
                        roleLaberl.backgroundColor = UIColor(red: 223/255.0, green: 247/255.0, blue: 255/255.0, alpha: 1.0)
                    
                        roleLaberl.text = "主播"
                        roleLaberl.font = UIFont.PingFang(size: 12)
                        roleLaberl.textColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1.0)
                    
                        //时间
                        let timeLaberl = UILabel()
                        timeLaberl.backgroundColor = UIColor.clear
                        timeLaberl.numberOfLines = 0;
                        vc.addSubview(timeLaberl)

                        
                        //聊天内容
                        let contentLaberl = UILabel()
                        //防止自动换行
                        contentLaberl.lineBreakMode = .byCharWrapping
                       
                        contentLaberl.font = UIFont.PingFang(size: 16)
               
                        contentLaberl.numberOfLines = 0;
                        vc.addSubview(contentLaberl)
                       
                       
                        //时间
                        let time:NSString = dict1.time as NSString
                        let currentDateStr = TalkfunSharedTools.getTimestamp(mStr: time)

                        let othersNameString = self.getAttributedString(str: currentDateStr, font: UIFont.PingFang(size: 14), color: UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0))
                        
                        timeLaberl.attributedText = othersNameString;
                        timeLaberl.textColor = UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0)
                        //其它人的名字
                        var nickname = dict1.nickname as NSString

                        if (nickname.length > 5) {
                            
                            nickname = nickname.substring(with: NSMakeRange(0, 5)) as NSString
                            nickname = nickname as String + "..." as NSString
                      
                        }
                        
                        let attrs: [NSAttributedString.Key : Any] = [.font: UIFont.PingFang(size: 17) as Any,.foregroundColor: UIColor.white, ]
                        
                        
                        let size = nickname.size(withAttributes: attrs)
                        let timeW:CGFloat = 48;
                        var nameW:CGFloat = size.width ;
                        if (size.width > self.viewWidth  - timeW - 79 - 19) {
                            nameW = self.viewWidth - 79 - 19 - timeW;
                        }
             
                        let nameLaberlString =  self.getAttributedString(str: nickname, font:  UIFont.PingFang(size: 17), color: UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0))
                        nameLaberl.attributedText = nameLaberlString;
                        nameLaberl.textColor = UIColor(red: 126/255.0, green: 133/255.0, blue: 151/255.0, alpha: 1.0)
                        let othersNameX:CGFloat = avatarImage1.frame.maxX + 7
                        
                       
                        let othersNameY:CGFloat = 0;
                        let nameH:CGFloat = 24;
                        nameLaberl.frame = CGRect(x: othersNameX, y: othersNameY, width: nameW, height: nameH)
             

                           if (dict1.role == .roleSpadmin||dict1.role == .roleAdmin) {
                                
                               if (dict1.role == .roleSpadmin) {
                                   roleLaberl.text = "主播";
                               }else if (dict1.role == .roleAdmin) {
                                    roleLaberl.text = "助教";
                               }
                                roleLaberl.isHidden = false
                               roleLaberl.frame = CGRect(x: nameLaberl.frame.maxX + 10, y: 4.5, width: 37, height: 18)
                    
                                roleLaberl.clipsToBounds = true
                                roleLaberl.layer.cornerRadius =  roleLaberl.frame.size.height/2;
                           }else{
                               roleLaberl.isHidden = true
                               roleLaberl.frame =  nameLaberl.frame;
                           }
                        //时间控件的位置
                        let timeH:CGFloat = nameH;
                        let timeX:CGFloat = roleLaberl.frame.maxX + 10;
                        let timeY:CGFloat = othersNameY;
                        timeLaberl.frame = CGRect(x: timeX, y: timeY, width: timeW, height: timeH)

                     let string =   self.getAttributedString(str: dict1.content as NSString, font: UIFont.PingFang(size: 16), color: UIColor(red: 77/255.0, green: 83/255.0, blue: 88/255.0, alpha: 1.0))
                        contentLaberl.attributedText = string;
                        
                        
                        let size11 = TalkfunSharedTools.getSizeLabel(labelString: string, labelWidth:self.viewWidth - 85 - 15, labelfont: UIFont.PingFang(size: 16))
                        
                        
                        
                        contentLaberl.frame = CGRect(x: othersNameX, y: nameLaberl.frame.maxY + 3, width: self.viewWidth - 85 - 15, height: size11.height)
                 
                        
                        nameLaberl.textAlignment    = .left;
                        contentLaberl.textAlignment =  .left;
                        
                    }
                    self.replyView.frame = CGRect(x: 0, y: self.content.frame.maxY, width: self.viewWidth, height: contentHight)
                
                }else{
                    self.replyView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
                }
                
                
            }
        }
    }
    func getAttributedString(str:NSString , font:UIFont , color:UIColor) -> NSMutableAttributedString
    {
        let othersNameStringC: [NSAttributedString.Key : Any] = [.font: font as Any,.foregroundColor: color, ]
        
        let othersNameString = NSMutableAttributedString.init(string: str as String, attributes: othersNameStringC)
        
        return othersNameString
    }
}
